﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GradeBookTest
{
    class GradeBook
    {
        private int[] grades;//arrary of student grades

        //auto-implemented property CourseName
        public string CourseName { get; set; }

        //two-parameter constructor initializes 
        //auto-implemented property CourseName and grades array
        public GradeBook(string name, int[] grade)
        {
            grades = grade;
            CourseName = name;
        }//end two-parameter constructor

        //Display a welcome message to GradeBook user
        public void DisplayMessage()
        {
            Console.WriteLine("Welcome to the grade book for\n {0}\n",CourseName);
        }//end method DisplayMessage

        //output the contents of the grades array
        public void OutputGrades()
        {
            Console.WriteLine("The grades are:\n");

            for (int i = 0; i < grades.Length; i++)
            {
                Console.WriteLine("Student {0}:{1,3}", i, grades[i]);
            }//end for

        }//end method OutputGrades

        //find maximum grade
        public int GetMaximum()
        {
            int max = grades[0];//assume grade[0] is  the greatest

            //loop through grades array
            foreach (int grade in grades)
            {
                //if grade greater than max,assign it to max
                if (grade > max)
                {
                    max = grade;
                }//end if
            }//end for

            return max;
        }//end method GetMaximum

        //find maximum grade
        public int GetMinimum()
        {
            int min = grades[0];//assume grades[0] is the lowest

            //loop in grades array 
            foreach (int grade in grades)
            {
                if (grade < min)
                {
                    min = grade;
                }//end if 
            }//end for

            return min;
        }//end method GetMinimum

        //output bar chart displaying grade distribution
        public void OutputBarChart()
        {
            Console.WriteLine("Grade Distribution:");

            //store frequency of grades in each range of 10 grades 
            int[] frequency = new int[11];

            //for each grade, increment the appropriate frequency
            foreach (int grade in grades)
            {
                ++frequency[grade / 10];
            }//end for

            //for each grade frequency, display bar in chart
            for (int i = 0; i < frequency.Length; i++)
            {
                if (10 == i)
                    Console.Write("100:");
                else
                    Console.Write("{0:D2}-{1:D2}:",i*10,i*10+9);

                for (int j = 0; j < frequency[i]; j++)
                {
                    Console.Write("*");
                }//end for

                Console.WriteLine();
            }//end outer for
        }//end method OutputBarChart 

        //determine average grade for test
        public double GetAverage()
        {
            int total = 0; // initialize total

            //sum grades for all student 
            foreach (int grade in grades)
            {
                total += grade;
            }//end for

            return (double)total / grades.Length;
        }//end method GetAverage

        //perform various operations on data
        public void ProcessGrades()
        {
            OutputGrades();

            Console.WriteLine("\nClass average is {0}", GetAverage());
            Console.WriteLine("Lowest grade is {0}\nHighest grade is {1}\n",GetMinimum(),GetMaximum());

            OutputBarChart();
        }//end method ProcessGrades
    }//end class GradeBook
}
